package com.isorf.controller;


import com.github.pagehelper.PageInfo;
import com.isorf.pojo.color;
import com.isorf.pojo.type;
import com.isorf.service.UploadService;
import com.isorf.service.typeservice;
import com.isorf.util.responseresult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class typecontroller {

    @Autowired
    private typeservice typeservice;
    @Autowired
    private UploadService uploadservice;
    @Value("${file.upload.path}")     // 从application.yml文件中获取图片上传的路径
    private String path;

    @PostMapping("/updatetype")
    public responseresult<Boolean> updatetype(@RequestBody type type ) {
        if (type == null || type.getTypeid() == null) {
            return responseresult.failure(responseresult.FAILED, "更新失败，缺少必要参数");
        }
        Boolean result = typeservice.updatetype(type);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "更新成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "更新失败");
        }
    }

    @PostMapping("/addtype")
    public responseresult<Boolean> addtype(@RequestBody type type) {
        Boolean result = typeservice.addtype(type);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "add successful", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "add failed");
        }

    }



    @PostMapping("/pageLLList")
    public responseresult<PageInfo<type>> pageList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) {
        PageInfo<type> result = typeservice.pageList(pageNum, pageSize);
        if (result==null&&result.getList().size()==0) {
            return responseresult.failure(responseresult.FAILED, "查询用户列表失败");
        }else {
            return responseresult.success(responseresult.SUCCESS,"查询成功", result);
        }
    }

    @PostMapping("/batchDDDelete")
    public responseresult<Boolean> batchDelete(@RequestBody List<Integer> ids)
    {
        // 检查参数
        if (ids == null || ids.isEmpty()) {
            return responseresult.failure(responseresult.FAILED, "删除失败，缺少必要参数");
        }

        // 调用service层的批量删除方法
        Boolean result = typeservice.batchDelete(ids);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "批量删除成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "批量删除失败");
        }


    }

    @PostMapping("/deletetype")
    public responseresult<Boolean> deletetype(@RequestBody type type) {
        // 检查link对象和id是否为空
        if (type == null || type.getTypeid() == null) {
            return responseresult.failure(responseresult.FAILED, "删除失败，缺少必要参数");
        }

        // 调用service层的删除方法，应该是一个删除方法而不是addlink
        Boolean result = typeservice.deletetype(type.getTypeid());
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "删除成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "删除失败");
        }
    }

    @GetMapping("/getalltype")
    public responseresult<List<type>> getalltype() {
        List<type> typeList = typeservice.getalltype();
        if (typeList == null) {
            return responseresult.failure(508, "没有学生信息");
        }else {
            return responseresult.success(200, "查询成功", typeList);

        }
    }

}
